package com.woniu.sharepark.leasesystem.tenant.service.impl;/**
 * @Author litong
 * @Date 2019/6/5 19:54
 */

import com.woniu.sharepark.common.JsonResult;
import com.woniu.sharepark.common.pojo.Login;
import com.woniu.sharepark.leasesystem.common.dao.OrderFormDAO;
import com.woniu.sharepark.leasesystem.common.pojo.OrderForm;
import com.woniu.sharepark.leasesystem.lessor.dao.CarParkDAO;
import com.woniu.sharepark.leasesystem.tenant.dao.CarDAO;
import com.woniu.sharepark.leasesystem.tenant.dao.ComplaintDAO;
import com.woniu.sharepark.leasesystem.tenant.pojo.Complaint;
import com.woniu.sharepark.leasesystem.tenant.service.ComplaintService;
import com.woniu.sharepark.leasesystem.tenant.vo.ComplaintVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 *@ClassName ComplaintImpl
 *@Description 投诉模块接口的实现类
 *@Author litong
 *@Date 2019/6/5 19:54
 *@Version 1.0
 **/
@Service
public class ComplaintImpl implements ComplaintService {

    @Resource
    private ComplaintDAO complaintDAO;
    @Resource
    private CarParkDAO carParkDAO;
    @Resource
    private OrderFormDAO orderFormDAO;
    @Resource
    private CarDAO carDAO;

    @Override
    public List allComplaints(Integer currentPage, Integer pageSize, ComplaintVO complaintVO) {
        return complaintDAO.queryComplaintByPage(currentPage,pageSize,complaintVO);
    }

    @Override
    public JsonResult addComplaint(Complaint complaint,Integer oId) {
      if(complaint.getCoReason()==null||complaint.getCoReason().equals("")){
          return JsonResult.error("投诉理由不能为空");
      }
        String oNumber=orderFormDAO.selectByPrimaryKey(oId).getoNumber();
       Complaint complaint1=complaintDAO.selectComplaintByONumber(oNumber);
       if (complaint1!=null){
           return JsonResult.error("您已针对该订单进行投诉，请耐心等待处理结果！");
       }else{
           //设置订单编号
           complaint.setoNumber(oNumber);
           //设置投诉方id
           Session session = SecurityUtils.getSubject().getSession();
           Login login = (Login) session.getAttribute("currentUser");
           int outUId=login.getUserInfo().getuId();
           complaint.setOutUId(outUId);
           //设置被投诉方id
           int cId=orderFormDAO.selectByPrimaryKey(oId).getcId();
           int inUId=carParkDAO.selectByPrimaryKey(cId).getUserInfo().getuId();
           complaint.setInUId(inUId);
           complaint.setCoTime(new Date());
           complaintDAO.addComplaint(complaint);
           return JsonResult.ok("添加投诉成功");
       }

    }

    @Override
    public JsonResult disposeComplaint(Integer coId) {
       int result= complaintDAO.disposeComplaint(coId);
       if (result==1){
           return JsonResult.ok("处理成功");
       }else {
           return JsonResult.error("处理失败");
       }

    }

    @Override
    public Integer countOfComplaints(ComplaintVO complaintVO) {
        return complaintDAO.selectCountOfComplaint(complaintVO);
    }

    @Override
    public JsonResult addComplaintToTenant(Complaint complaint, Integer oId) {
        if(complaint.getCoReason()==null||complaint.getCoReason().equals("")){
            JsonResult.error("投诉理由不能为空");
        }
        String oNumber=orderFormDAO.selectByPrimaryKey(oId).getoNumber();
        Complaint complaint1=complaintDAO.selectComplaintByONumber(oNumber);
        if (complaint1!=null){
           return JsonResult.error("您已针对该订单进行投诉，请耐心等待处理结果！");
        }else{
            //设置订单编号
            complaint.setoNumber(oNumber);
            //设置投诉方id
            Session session = SecurityUtils.getSubject().getSession();
            Login login = (Login) session.getAttribute("currentUser");
            int outUId=login.getUserInfo().getuId();
            complaint.setOutUId(outUId);
            //设置被投诉方id
            int carId=orderFormDAO.selectByPrimaryKey(oId).getCarId();
            int inUId=carDAO.selectByPrimaryKey(carId).getuId();
            complaint.setInUId(inUId);
            complaint.setCoTime(new Date());
            complaintDAO.addComplaint(complaint);
            return JsonResult.ok("添加投诉成功");
        }

    }
}
